<template>
  <div id="globalSider">
    <div v-if="loginUserStore.loginUser.id" class="sider-background">
      <div class="gradient-orb orb1"></div>
      <div class="gradient-orb orb2"></div>
      <div class="particles-container">
        <div
          v-for="n in 20"
          :key="n"
          class="particle"
          :style="{
            '--delay': `${Math.random() * 15}s`,
            '--size': `${Math.random() * 4 + 1}px`,
            '--start-x': `${Math.random() * 100}%`,
            '--start-y': `${Math.random() * 100}%`,
            '--color': `hsl(${Math.random() * 40 + 20}, 80%, 70%)`,
          }"
        ></div>
      </div>
    </div>
    <a-layout-sider
      v-if="loginUserStore.loginUser.id"
      width="240"
      breakpoint="lg"
      collapsed-width="0"
      class="custom-sider"
      :collapsed="collapsed"
    >
      <a-menu
        v-model:selectedKeys="current"
        mode="inline"
        :items="menuItems"
        @click="doMenuClick"
        class="custom-menu"
      />
    </a-layout-sider>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref, watchEffect } from 'vue'
import { PictureOutlined, TeamOutlined, UserOutlined } from '@ant-design/icons-vue'
import { useRouter, useRoute } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { SPACE_TYPE_ENUM } from '@/constants/space'
import { listMyTeamSpaceUsingPost } from '@/api/spaceUserController'
import { message } from 'ant-design-vue'

const loginUserStore = useLoginUserStore()
const router = useRouter()
const route = useRoute()

// 侧边栏折叠状态
const collapsed = computed(() => {
  // 在社区相关页面自动折叠侧边栏
  return route.path.includes('/community')
})

// 固定的菜单列表
const fixedMenuItems = [
  {
    key: '/',
    icon: () => h(PictureOutlined),
    label: '公共图库',
  },
  {
    key: '/my_space',
    label: '我的空间',
    icon: () => h(UserOutlined),
  },
  {
    key: '/add_space?type=' + SPACE_TYPE_ENUM.TEAM,
    label: '创建团队',
    icon: () => h(TeamOutlined),
  },
]

const teamSpaceList = ref<API.SpaceUserVO[]>([])
const menuItems = computed(() => {
  // 如果用户没有团队空间，则只展示固定菜单
  if (teamSpaceList.value.length < 1) {
    return fixedMenuItems
  }
  // 如果用户有团队空间，则展示固定菜单和团队空间菜单
  // 展示团队空间分组
  const teamSpaceSubMenus = teamSpaceList.value.map((spaceUser) => {
    const space = spaceUser.space
    return {
      key: '/space/' + spaceUser.spaceId,
      label: space?.spaceName,
    }
  })
  const teamSpaceMenuGroup = {
    type: 'group',
    label: '我的团队',
    key: 'teamSpace',
    children: teamSpaceSubMenus,
  }
  return [...fixedMenuItems, teamSpaceMenuGroup]
})

// 加载团队空间列表
const fetchTeamSpaceList = async () => {
  const res = await listMyTeamSpaceUsingPost()
  if (res.data.code === 0 && res.data.data) {
    teamSpaceList.value = res.data.data
  } else {
    message.error('加载我的团队空间失败，' + res.data.message)
  }
}

/**
 * 监听变量，改变时触发数据的重新加载
 */
watchEffect(() => {
  // 登录才加载
  if (loginUserStore.loginUser.id) {
    fetchTeamSpaceList()
  }
})

// 当前要高亮的菜单项
const current = ref<string[]>([])
// 监听路由变化，更新高亮菜单项
router.afterEach((to, from, next) => {
  current.value = [to.path]
})

// 路由跳转事件
const doMenuClick = ({ key }) => {
  router.push(key)
}
</script>

<style scoped>
#globalSider {
  position: relative;
  z-index: 999;
}

.sider-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 240px;
  height: 100vh;
  overflow: hidden;
  z-index: -1;
  pointer-events: none;
}

.gradient-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(60px);
  opacity: 0.3;
  mix-blend-mode: soft-light;
  animation: floatOrb 20s infinite ease-in-out;
}

.orb1 {
  width: 300px;
  height: 300px;
  background: radial-gradient(circle at 30% 30%, #ff9f43, rgba(255, 138, 101, 0));
  top: 10%;
  left: -50%;
  animation-duration: 25s;
}

.orb2 {
  width: 250px;
  height: 250px;
  background: radial-gradient(circle at 70% 30%, #ffd6a5, rgba(255, 214, 165, 0));
  bottom: 20%;
  left: -30%;
  animation-duration: 30s;
  animation-delay: 2s;
}

.particles-container {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.particle {
  position: absolute;
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  background: var(--color);
  left: var(--start-x);
  top: var(--start-y);
  animation: floatParticle var(--delay) infinite linear;
  opacity: 0.4;
}

.custom-sider {
  background: rgba(255, 255, 255, 0.85) !important;
  backdrop-filter: blur(12px);
  box-shadow: 4px 0 20px rgba(31, 38, 135, 0.1);
  border-right: 1px solid rgba(255, 159, 67, 0.1);
  height: 100%;
  transition: all 0.3s ease;
}

.custom-sider :deep(.ant-layout-sider-children) {
  display: flex;
  flex-direction: column;
  padding: 20px 12px;
}

.custom-menu {
  background: transparent !important;
  border-right: none !important;
}

.custom-menu :deep(.ant-menu-item) {
  height: 48px !important;
  line-height: 48px !important;
  margin: 4px 8px !important;
  border-radius: 12px !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  background: rgba(255, 255, 255, 0.5);
}

.custom-menu :deep(.ant-menu-item:hover) {
  background: rgba(255, 159, 67, 0.1) !important;
  transform: translateX(4px);
}

.custom-menu :deep(.ant-menu-item-selected) {
  background: linear-gradient(135deg, rgba(255, 159, 67, 0.2), rgba(255, 127, 80, 0.2)) !important;
  color: #ff7f50 !important;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(255, 159, 67, 0.1);
}

.custom-menu :deep(.ant-menu-item-group-title) {
  color: #ff9f43 !important;
  font-size: 13px;
  padding: 24px 16px 8px !important;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  opacity: 0.8;
}

.custom-menu :deep(.anticon) {
  color: #ff9f43 !important;
  font-size: 18px;
  margin-right: 12px;
  transition: all 0.3s;
}

.custom-menu :deep(.ant-menu-submenu-arrow) {
  color: #ff9f43 !important;
  opacity: 0.8;
}

@keyframes floatParticle {
  0% {
    transform: translate(0, 0) rotate(0deg);
    opacity: 0;
  }
  20% {
    opacity: 0.4;
  }
  80% {
    opacity: 0.4;
  }
  100% {
    transform: translate(calc(sin(var(--delay)) * 100px), calc(cos(var(--delay)) * 100px))
      rotate(360deg);
    opacity: 0;
  }
}

@keyframes floatOrb {
  0%,
  100% {
    transform: translate(0, 0) scale(1);
  }
  25% {
    transform: translate(20px, 20px) scale(1.1);
  }
  50% {
    transform: translate(-20px, -10px) scale(0.9);
  }
  75% {
    transform: translate(10px, 20px) scale(1.05);
  }
}

@media (max-width: 992px) {
  .custom-sider {
    position: fixed !important;
    z-index: 1001;
    box-shadow: 8px 0 32px rgba(255, 159, 67, 0.15);
  }

  .sider-background {
    position: fixed;
    left: -240px;
    transition: all 0.3s ease;
  }
}

@keyframes menuItemEntrance {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.custom-menu :deep(.ant-menu-item) {
  animation: menuItemEntrance 0.4s ease forwards;
}
</style>
